package com.yoyoyo666.report.controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.yoyoyo666.report.common.AnnoLogin;
import com.yoyoyo666.report.common.DateUtils;
import com.yoyoyo666.report.common.IDUtils;
import com.yoyoyo666.report.mapper.ReportContentMapper;
import com.yoyoyo666.report.mapper.ReportPlanMapper;
import com.yoyoyo666.report.model.LoginUser;
import com.yoyoyo666.report.model.ReportContent;

import com.yoyoyo666.report.model.ReportPlan;
import com.yoyoyo666.report.view.EasypoiMoreSheetExcelView;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;


@Controller
@RequestMapping("export")
public class ExportController {





    @Autowired
    private ReportContentMapper contentMapper;
    @Autowired
    private ReportPlanMapper planMapper;

    private DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @RequestMapping("/exportReport1")
    public String exportMerchantProfitQuery(@AnnoLogin LoginUser reportUser,ModelMap modelMap, HttpServletRequest request) {

        List<ReportContent> reportContents = contentMapper.selectAll(reportUser.getCurrentUser().getId(),null);
        ExportParams exportParams = new ExportParams("技术部"+ reportUser.getCurrentUser().getName() +"工作计划", "个人工作记录");
        modelMap.put(NormalExcelConstants.CLASS, ReportContent.class);
        modelMap.put(NormalExcelConstants.FILE_NAME, "日报-" + reportUser.getCurrentUser().getName() + "-" + sdf.format(new Date()) );
        modelMap.put(NormalExcelConstants.PARAMS, exportParams);
        modelMap.put(NormalExcelConstants.DATA_LIST, reportContents);

        List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> pr = new HashMap<>();
        pr.put("title",exportParams);
        pr.put("entity",ReportContent.class);
        pr.put("data",reportContents);
        list.add(pr);

        List<ReportPlan> plan = planMapper.selectAll(reportUser.getCurrentUser().getId(),null);
        ExportParams exportParams1 = new ExportParams("技术部"+ reportUser.getCurrentUser().getName() +"工作计划", "工作计划");
        Map<String,Object> pr1 = new HashMap<>();
        pr1.put("title",exportParams1);
        pr1.put("entity",ReportPlan.class);
        pr1.put("data",plan);
        list.add(pr1);
        modelMap.put("list",list);


        return EasypoiMoreSheetExcelView.EASYPOI_MORE_SHEET_EXCEL_VIEW;
    }

    @RequestMapping("/exportReport")
    public String exportReport1(@AnnoLogin LoginUser reportUser,ModelMap modelMap, HttpServletRequest request) {

        modelMap.put(NormalExcelConstants.CLASS, ReportContent.class);
        modelMap.put(NormalExcelConstants.FILE_NAME, "日报-" + reportUser.getCurrentUser().getName() + "-" + sdf.format(new Date()) );
        modelMap.put("user",reportUser.getCurrentUser());
        return EasypoiMoreSheetExcelView.EASYPOI_MORE_SHEET_EXCEL_VIEW;
    }

    @RequestMapping("fileUpload")
    @ResponseBody
    public String  fileUpload(@RequestParam("file") CommonsMultipartFile file,@AnnoLogin LoginUser user) throws Exception {
        ImportParams importParams = new ImportParams();
        importParams.setTitleRows(1);
        InputStream input = new ByteArrayInputStream(file.getBytes());
        List<ReportContent> objects = ExcelImportUtil.importExcel(input, ReportContent.class, importParams);
        for (ReportContent object : objects) {
            reSetTime(object);
            object.setId(IDUtils.getId());
            object.setUserId(user.getCurrentUser().getId());

            Date creatTime = DateUtils.parse(object.getCreateTime());
            if(creatTime!=null){
                String format = DateUtils.sdf.format(creatTime);
                object.setUpdateTime(format);
            }else {
                object.setUpdateTime(DateUtils.getNowString());
            }
            contentMapper.insert(object);
        }
        return "成功";
    }


    private ReportContent reSetTime(ReportContent object){
        Date parse = DateUtils.parse(object.getCreateTime());
        if(parse != null){
            object.setCreateTime(DateUtils.sdf2.format(parse));
        }
        Date parse1 = DateUtils.parse(object.getStartTime());
        if(parse1 != null){
            object.setStartTime(DateUtils.sdf2.format(parse1));
        }
        Date parse2 = DateUtils.parse(object.getEndTime());
        if(parse2 != null){
            object.setEndTime(DateUtils.sdf2.format(parse2));
        }
        return object;
    }
}
